package ll1.analyzer;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import ll1.grammars.GrammarException;
import ll1.grammars.LL1Grammar;


public class TP2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			System.out.println("[+]");
			System.out.println("Parser de Gramáticas LL(1)");
			System.out.println("--------------------------\n\n");
			
			LL1Grammar grammar = new LL1Grammar();
			grammar.getProductions("grammar.txt");
			
			System.out.println("\n");
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			System.out.print("Ingrese una palabra: ");
			String line = br.readLine();
			
			while( line !=null && line.length()!=0 ){
				if(grammar.validate(line.trim())){
					System.out.println("true");
				}
				else{
					System.out.println("false");
				}
				
				System.out.print("Ingrese una palabra: ");
				line = br.readLine();	
			}
			
			
			System.out.println("\n\nFinalizado");
			System.out.println("[+]");
			
		} catch (FileNotFoundException e) {
			System.out.println("Error al abrir el archivo" + ((args.length==0)?"grammar.txt":args[1]) );
		} catch (IOException e) {
			System.out.println("Error al abrir el archivo" + ((args.length==0)?"grammar.txt":args[1]) );
		} catch (GrammarException e) {
			System.out.println(e.getMessage());
		} catch (Exception e){
			System.out.println("El programa ha finalizado abruptamente");
		}

	}

}
